<?php
declare (strict_types=1);

namespace app\model;

use think\facade\Log;
use think\Model;

/**
 * @mixin \think\Model
 */
class Activity extends Model
{
    //
   // protected $type = ['category' => 'json','shop_type'=>'json','specified_shop_ids'=>'json'];
    protected $type = ['category' => 'json','shop_type'=>'json'];

    protected $append = ['status', 'status_desc'];

    public function getApplyStartTimeAttr($applyStartTime)
    {
        return date('Y-m-d', $applyStartTime);
    }

    public function getApplyEndTimeAttr($applyStartTime)
    {
        return date('Y-m-d', $applyStartTime);
    }

    public function getPromoteStartTimeAttr($promoteStartTime)
    {
        return date('Y-m-d', $promoteStartTime);
    }

    public function getPromoteEndTimeAttr($promoteEndTime)
    {
        return date('Y-m-d', $promoteEndTime);
    }

    public function getCommissionRateAttr($commissionRate)
    {
        return round($commissionRate, 2);
    }

    public function getServiceRateAttr($serviceRate)
    {
        return round($serviceRate, 2);
    }

    public function getStatusAttr($status)
    {
        // 状态 1未上线 2报名未开始 3报名中 4 推广未开始 5推广中 6已结束
        if ($this->online == 0) {
            return 1;
        } else {
            $time = time();
            if ($time < strtotime($this->apply_start_time)) {
                return 2;
            }
            if ($time >= strtotime($this->apply_start_time) && $time < (strtotime($this->apply_end_time) + 86399)) {
                return 3;
            }
            if ($time < strtotime($this->promote_start_time)) {
                return 4;
            }
            if ($time >= strtotime($this->promote_start_time) && $time < (strtotime($this->promote_end_time) + 86399)) {
                return 5;
            }
            if ($time >= (strtotime($this->promote_end_time) + 86399)) {
                return 6;
            }
        }
        return -1;
    }

    public function getStatusDescAttr($statusDesc)
    {

        if ($this->online == 0) {
            return '未上线';
        } else {
            $time = time();
            if ($time < strtotime($this->apply_start_time)) {
                return '报名未开始';
            }
            if ($time >= strtotime($this->apply_start_time) && $time < (strtotime($this->apply_end_time) + 86399)) {
                return '报名中';
            }
            if ($time < strtotime($this->promote_start_time)) {
                return '推广未开始';
            }
            if ($time >= strtotime($this->promote_start_time) && $time < (strtotime($this->promote_end_time) + 86399)) {
                return '推广中';
            }
            if ($time >= (strtotime($this->promote_end_time) + 86399)) {
                return '已结束';
            }
        }
        return '未知';
    }
}
